import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { classToPlain } from 'class-transformer';

@Injectable()
export class TransformInterceptor implements NestInterceptor {
  constructor() {}
  intercept(
    _context: ExecutionContext,
    next: CallHandler<any>,
  ): Observable<any> | Promise<Observable<any>> {
    console.log('haha: ', _context.getArgByIndex(1).req);
    return next.handle().pipe(
      map((data: any) => {
        return {
          result: classToPlain(data),
          code: 0,
          message: '请求成功！',
          token: '',
        };
      }),
    );
  }
}
